/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.interfaces.swt;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.TableItem;

import ws.y10k.goloim.model.Box;
import ws.y10k.goloim.model.BoxAlreadyExistsException;
import ws.y10k.goloim.model.BoxDoesNotExistException;

/**
 * @author natx
 *
 */
public class SaveBoxSelectionListener implements SelectionListener {
	
	private ManageBoxesWindow parent;
	
	public SaveBoxSelectionListener(ManageBoxesWindow parent) {
		this.parent = parent;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetDefaultSelected(SelectionEvent arg0) { }

	/* (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
	public void widgetSelected(SelectionEvent arg0) {
		// TODO i18n
		if ( parent.txtWidth.getText().length() > 0 && parent.txtHeight.getText().length() > 0 && parent.txtDepth.getText().length() > 0 && parent.txtWeight.getText().length() > 0 ) {
			double width;
			try {
				width = Double.parseDouble(parent.txtWidth.getText());
			} catch (NumberFormatException e) {
				MessageDialog.openError(parent.getShell(), "Format error in width field", "Width field must be a number.");
				return;
			}
			double height;
			try {
				height = Double.parseDouble(parent.txtHeight.getText());
			} catch (NumberFormatException e) {
				MessageDialog.openError(parent.getShell(), "Format error in height field", "Height field must be a number.");
				return;
			}
			double depth;
			try {
				depth = Double.parseDouble(parent.txtDepth.getText());
			} catch (NumberFormatException e) {
				MessageDialog.openError(parent.getShell(), "Format error in depth field", "Depth field must be a number.");
				return;
			}
			double weight;
			try {
				weight = Double.parseDouble(parent.txtWeight.getText());
			} catch (NumberFormatException e) {
				MessageDialog.openError(parent.getShell(), "Format error in weight field", "Weight field must be a number.");
				return;
			}
			try {
				Box box = new Box(width,height,depth,weight);

				if ( parent.itmSelected != null ) {
					try {
						Box oldBox = GoLoIm.boxes.getBox(Double.parseDouble(parent.itmSelected.getText(0)),Double.parseDouble(parent.itmSelected.getText(1)),Double.parseDouble(parent.itmSelected.getText(2)),Double.parseDouble(parent.itmSelected.getText(3)));
						oldBox.setWidth(width);
						oldBox.setHeight(height);
						oldBox.setDepth(depth);
						oldBox.setWeight(weight);
						String[] values = {((Double) width).toString(),((Double) height).toString(),((Double) depth).toString(),((Double) weight).toString()};
						parent.itmSelected.setText(values);
						parent.itmSelected = null;
						parent.tblBoxes.deselectAll();
					} catch (BoxDoesNotExistException e) { }
				} else {
					GoLoIm.boxes.add(box);
					TableItem itmBox = new TableItem(parent.tblBoxes, SWT.NONE);
					itmBox.setText(0,((Double) box.getWidth()).toString());
					itmBox.setText(1,((Double) box.getHeight()).toString());
					itmBox.setText(2,((Double) box.getDepth()).toString());
					itmBox.setText(3,((Double) box.getWeight()).toString());
				}
				parent.txtWidth.setText("");
				parent.txtHeight.setText("");
				parent.txtDepth.setText("");
				parent.txtWeight.setText("");
				parent.txtWidth.setFocus();
			} catch (BoxAlreadyExistsException e) {
				MessageDialog.openError(parent.getShell(), "Box already exists", "A box like that already exists.");
			}
		} else {
			MessageDialog.openError(parent.getShell(), "Not enough information", "You must specify width, height, depth and weight of the box.");
		}
		
	}

}
